package com.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;


import com.listener.HibernateListener;
import com.opensymphony.xwork2.ActionSupport;
import com.pojo.County;
import com.pojo.Government;
import com.pojo.Programme;
import com.vo.GovernmentVO;

public class GovernmentDAO extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@SuppressWarnings("unchecked")
	public List<GovernmentVO> list(String isActive) {
		@SuppressWarnings("static-access")
		Session session = new HibernateListener().getSessionFactory()
				.openSession();
		session.beginTransaction();
		List<GovernmentVO> listGov = null;
		String sql;
		if (isActive.equals("No")) {
			sql = "select new com.vo.GovernmentVO(a.governmentId, a.governmentName, a.description, a.county.countyName, a.isActive)"
					+ " from Government as a";
		} else {
			sql = "select new com.vo.GovernmentVO(a.governmentId, a.governmentName, a.description, a.county.countyName, a.isActive)"
					+ " from Government as a where a.isActive like '" + isActive + "'";
		}
		Query query = session.createQuery(sql);
		listGov = query.list();
		session.getTransaction().commit();
		session.close();
		return listGov;
	}

	@SuppressWarnings("unchecked")
	public GovernmentVO getGovernmentById(int governmentId) {
		String sql = "select new com.vo.GovernmentVO (a.governmentId, a.governmentName, a.description, a.county.countyName, a.isActive)"
				+ "from Government as a where governmentId = " + governmentId;
		Session session = HibernateListener.getSessionFactory().openSession();
		session.beginTransaction();
		Query query = session.createQuery(sql);
		List<GovernmentVO> listGo = query.list();
		session.getTransaction().commit();
		return listGo.get(0);
	}

	public void delete(int governmentId) {
		GovernmentVO g = getGovernmentById(governmentId);
		County c = new CountyDAO().getCountyByCountyName(g.getCountyName());
		Session session = HibernateListener.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		Government g1 = new Government();
		g1.setGovernmentId(governmentId);
		g1.setGovernmentName(g.getGovernmentName());
		g1.setDescription(g.getDescription());
		g1.setCounty(c);
		g1.setIsActive(g.getIsActive());
		session.delete(g1);
		transaction.commit();
		session.close();
	}

	public void insert(Government gov) {
		Session session = HibernateListener.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		session.save(gov);
		transaction.commit();
		session.close();
	}

	public void updateGoverment(GovernmentVO gov, int governmentId) {
		Session session = HibernateListener.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		Government g = new Government();
		g = (Government) session.get(Government.class, governmentId);
		transaction.commit();

		session.close();
		g.setGovernmentName(gov.getGovernmentName());
		g.setDescription(gov.getDescription());
		g.setCounty(new CountyDAO().getCountyByCountyName(gov.getCountyName()));
		g.setIsActive(gov.getIsActive());

		Session session2 = HibernateListener.getSessionFactory().openSession();
		transaction = session2.beginTransaction();

		session2.update(g);
		transaction.commit();
		session2.close();
	}
	
	@SuppressWarnings("unchecked")
	public List<GovernmentVO> getByFilterName(String filterName, String isActive){
		List<GovernmentVO> list = new ArrayList<GovernmentVO>();
		Session session = HibernateListener.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		String sql;
		if (isActive.equals("No")) {
			sql = "select new com.vo.GovernmentVO (a.governmentId, a.governmentName, a.description, a.county.countyName, a.isActive) "
					+ " from Government as a where a.governmentName like '"
					+ filterName + "%'";
		} else {
			sql = "select new com.vo.GovernmentVO (a.governmentId, a.governmentName, a.description, a.county.countyName, a.isActive) "
					+ " from Government as a where a.governmentName like '"
					+ filterName + "%' and a.isActive like '" + isActive + "'";
		}
		Query query = session.createQuery(sql);
		list = query.list();
		transaction.commit();
		session.close();
		return list;
	}
	
	public boolean checkGovernmentName(String governmentName) {
		Session session = HibernateListener.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		String sql = "from Government where governmentName='" + governmentName
				+ "'";
		Query query = session.createQuery(sql);
		List<Government> list = query.list();
		if (list.size() > 0) {
			return false;
		}
		return true;

	}
}
